// pages/orderCenter/orderCenter.js
let http = require('../../utils/request.js')
let path = getApp().globalData.path

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: '0',
    name: '订单中心',
    baseurl: path,
    page: 1,
    tmp: []
  },
  // 全部详情
  goAllDetail(e) {
    let orderid=e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `/pages/allDetail/allDetail?orderid=${orderid}`,
    })
  },
  // 跳转待支付详情
  goPayDetail(e) {
    let orderid=e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `/pages/w-pay-detail/w-pay-detail?orderid=${orderid}`,
    })
  },
  // 跳转待发货详情
  goSendDetail(e) {
    let orderid=e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `/pages/w-send-detail/w-send-detail?orderid=${orderid}`,
    })
  },
  // 跳转待收货详情
  goGetDetail(e) {
    let orderid=e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `/pages/w-get-detail/w-get-detail?orderid=${orderid}`,
    })
  },
  // 跳转待评论详情
  goCommentDetail(e) {
    let orderid=e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `/pages/w-talk-detail/w-talk-detail?orderid=${orderid}`,
    })
  },
  // 退款
  goRefund(e) {
    let orderid = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `/pages/refund/refund?orderid=${orderid}`,
    })
  },
  // 确认收货
  goConfirm(e) {
    let orderid = e.currentTarget.dataset.orderid
    http.postHttp('/user_order_status', {
      order_id: orderid,
      status: 3
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log(res, '确认收货')
      if (res.data.code == 1) {
        http.getHttp('/user_order_list', {
          status: 3,
          type: 1,
          page: 1
        }, {
          token: wx.getStorageSync('token')
        }).then(res => {
          let tmpArr = res.data.data
          console.log(tmpArr)
          this.setData({
            tmp: tmpArr
          })
        })
      }
    })
  },
  // 取消订单
  goCancel(e) {
    let _this=this
    wx.showModal({
      title: '取消订单',
      content: '确定要取消该订单？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: 'skyblue', //取消文字的颜色
      confirmText: "是", //默认是“确定”
      confirmColor: 'black', //确定文字的颜色
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          let orderid = e.currentTarget.dataset.orderid
          http.postHttp('/user_order_status', {
            order_id: orderid,
            status: 0
          }, {
            token: wx.getStorageSync('token')
          }).then(res => {
            console.log(res, '取消订单')
            http.getHttp('/user_order_list', {
              status: _this.data.type,
              type: 1,
              page: 1
            }, {
              token: wx.getStorageSync('token')
            }).then(res => {
              let tmpArr = res.data.data
              console.log(tmpArr)
              _this.setData({
                tmp: tmpArr
              })
            })
          })
          
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })



  },
  // 付款
  goPay(e) {
    let orderid = e.currentTarget.dataset.orderid
    http.postHttp('/user_order_pay', {
      order_id: orderid
    }, {}).then(res => {
      console.log(res, '支付凭据')
      wx.requestPayment({
        timeStamp: res.data.data.timeStamp,
        nonceStr: res.data.data.nonceStr,
        package: res.data.data.package,
        signType: 'MD5',
        paySign: res.data.data.paySign,
        success(res) {
          console.log(res, '支付返回')
          // wx.showToast({
          //   title: '购买成功',
          //   icon:'none',
          //   duration:2000
          // })
          wx.navigateTo({
            url: '/pages/w-send-detail/w-send-detail',
          })
        },
        fail(res) {}
      })
    })
  },
  // 联系客服
  goService(e) {
    wx.makePhoneCall({
      phoneNumber: '18011524442',
      success() {
        console.log('拨打成功')
      },
      fail() {
        console.log('拨打失败')
      }
    })
  },
  // tab切换加载
  onChange(event) {
    let type = event.detail.name
    wx.setStorageSync('tabType', event.detail.name)
    this.setData({
      type
    })
    console.log(type, 'type')
    http.getHttp('/user_order_list', {
      status: type,
      type: 1
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log(res.data.data)
      let tmpArr = res.data.data
      this.setData({
        tmp: tmpArr
      })

    })
  },
  // 评论
  goComment(e) {
    // let _this=this
    // wx.setStorageSync('tabType', _this.data.type)
    let orderid = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: `/pages/comment/comment?orderid=${orderid}`,
    })
  },

  getMore(e) {
    var that = this;
    if (that.data.page == 1) {
      wx.showLoading({
        title: ' 加载中',
        make: true
      })
    }
    http.getHttp('/user_order_list', {
      status: 0,
      type: 1,
      page: 1,
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      if (that.data.page > 1) {
        let tmp = that.data.tmp;
        let page = that.data.page
        console.log(tmp, 'tmp')
        // this.getMore()
        that.setData({
          tmp: tmp.concat(res.data.data),
          page: page + 1
        })
      } else {
        let page = that.data.page
        // this.getMore(that.data.page)
        that.setData({
          tmp: res,
          page: page + 1
        })
      }

      console.log(res, 'res')
      // 回调函数
      // var moment_list = that.data.moment;
      // const oldData = that.data.tmp;
      // that.setData({
      //   tmp: oldData.concat(res.data.data)
      // })
      // 隐藏加载框
      wx.hideLoading();
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // this.getMore(that.data.page);
    http.getHttp('/user_order_list', {
      status: 0,
      type: 1,
      page: 1
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      let tmpArr = res.data.data
      console.log(tmpArr)
      this.setData({
        tmp: tmpArr
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let tabType = wx.getStorageSync('tabType')
    if (tabType !== 3) {
      http.getHttp('/user_order_list', {
        status: tabType,
        type: 1
      }, {
        token: wx.getStorageSync('token')
      }).then(res => {
        let tmpArr = res.data.data
        // console.log(tmpArr)
        this.setData({
          tmp: tmpArr,
        })
      })
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    let hot = that.data.hot
    //上拉加载
    wx.showLoading({
      title: '正在加载',
    })


    //加载次数加一
    this.setData({
      page: this.data.page + 1
    })

    http.getHttp('/user_trade_log', {
      status:that.data.type,
      page: that.data.page,
      type:1
    }, {}).then(res => {
      console.log('加载更多', res)
      if (res.data.code == 200 && res.data.data.length !== 0) {
        hot = hot.concat(res.data.data)
        //往前台传递数据
        that.setData({
          tmp
        })
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '没有更多了喔~',
          icon: 'none'
        })
      }
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})